package com.lenovo.smarttraffic.zm_user_module;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager_Peccancy;
import com.lenovo.smarttraffic.database.dao.UserPeccancyBeanDao;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.fragment.fgBean.DragReturnBean;

import org.greenrobot.eventbus.EventBus;

import java.util.Hashtable;
import java.util.List;

public class QRcodeActivity extends BaseActivity {

    private ImageView iv_qr_Code;
    private TextView tv_qrCode;
    private String userName;
    private int position;
    String TAG = "QRcodeActivity";
    private List<UserPeccancyBean> list;
    private UserPeccancyBean userPeccancyBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        userName = intent.getStringExtra("listName");
        position = intent.getIntExtra("position", 0);
        DaoManager_Peccancy.getmInstance_Peccancy().init(this);
        UserPeccancyBeanDao beanDao = DaoManager_Peccancy.getmInstance_Peccancy().getDaoSession_Peccancy().getUserPeccancyBeanDao();
        list = beanDao.queryBuilder().where(UserPeccancyBeanDao.Properties.Username.eq(userName)).list();
        userPeccancyBean = list.get(position);
        initToolBar(findViewById(R.id.toolbar), true, "违章支付");
        tv_qrCode = (TextView) findViewById(R.id.tv_qrCode);
        iv_qr_Code = (ImageView) findViewById(R.id.iv_qr_Code);
        iv_qr_Code.setImageBitmap(createQRCode("罚款" + userPeccancyBean.getPmoney(), 500, 500, "UTF-8", "H", "1", Color.BLACK, Color.WHITE));
        iv_qr_Code.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                tv_qrCode.setText("罚款" + userPeccancyBean.getPmoney());
                userPeccancyBean.setHanlderFlag(true);
                Log.e(TAG, "onLongClick: " + userPeccancyBean.getHanlderFlag());
                beanDao.update(userPeccancyBean);
                EventBus.getDefault().post(list);
                return true;
            }
        });


    }

    @Override
    protected int getLayout() {
        return R.layout.activity_qrcode;
    }
    //    imageView.setImageBitmap(createQRCode("demo",800,800,"UTF-8","H","1",Color.BLACK,Color.WHITE));


    public Bitmap createQRCode(String content, int width, int height, String character_set, String error_correction_level, String margin, int color_black, int color_white) {
        if (TextUtils.isEmpty(content)) {
            return null;
        }
        if (width <= 0 || height <= 0) {
            return null;
        }
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        if (!TextUtils.isEmpty(character_set)) {
            hints.put(EncodeHintType.CHARACTER_SET, character_set);
        }
        if (!TextUtils.isEmpty(error_correction_level)) {
            hints.put(EncodeHintType.ERROR_CORRECTION, error_correction_level);
        }
        if (!TextUtils.isEmpty(margin)) {
            hints.put(EncodeHintType.MARGIN, margin);
        }
        try {
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = color_black;
                    } else {
                        pixels[y * width + x] = color_white;
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }
}
